package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.DealerService;
import com.bed.lovewithpatientbed.service.DeptManagerService;
import com.bed.lovewithpatientbed.service.EmployeeService;
import com.bed.lovewithpatientbed.service.HospitalManagerService;
import com.bed.lovewithpatientbed.util.LayUiUtil;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author myp
 * @version 1.0.0
 * @ClassName WorkerController.java
 * @Description 雇员管理Controller
 */
@Controller
@RequestMapping("worker")
public class WorkerController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private DealerService dealerService;
    @Resource
    private HospitalManagerService hospitalManagerService;
    @Resource
    private DeptManagerService deptManagerService;
    //=============================================雇员控制==============================================================
    /**
     * @title 增加雇员
     * @description 前端传入雇员类，返回数量确认是否增加成功；
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("saveEmployeeDo")
    @ResponseBody
    public Map<String,Object> saveEmployeeDo(Employee employee){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i= employeeService.saveEmployee(employee);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 删除雇员的控制器
     * @description 前端传入雇员类，返回数量确认是否删除成功；
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("delEmployeeDo")
    @ResponseBody
    public Map<String,Object> delEmployeeDo(Employee employee){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i= employeeService.delEmployee(employee);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 修改雇员信息的控制器
     * @description 前端传入雇员类，返回数量确认是否修改成功；
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("updateEmployeeDo")
    @ResponseBody
    public Map<String,Object> updateEmployeeDo(Employee employee){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i= employeeService.updateEmployee(employee);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 查询雇员列表信息
     * @description 条件组合查询。传入页面信息，雇员类，起止时间。返回查询出的列表
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectEmployeeListDo")
    @ResponseBody
    public Map<String,Object> selectEmployeeListDo(LayUiPageBean pageBean, Employee employee, @DateTimeFormat(pattern = "yyyy-MM-dd") Date staData ,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        Map<String,Object> resultMap = null;
        try{
            List<Employee> employeeList = employeeService.selectEmployeeList(pageBean, employee, staData, endDate);
            Integer count=employeeService.getCount(employee, staData, endDate);
            resultMap = LayUiUtil.getLayTableResponseMap("success", 0, count, employeeList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap = LayUiUtil.getLayTableResponseMap("failed", 400, 0, null);
        }
        return resultMap;
    }
    /**
     * @title 查询雇员
     * @description 按照雇员编号查找雇员
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectEmployeeDo")
    @ResponseBody
    public Map<String,Object> selectEmployeeDO(Integer employeeId){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Employee selectEmployee = employeeService.selectEmployee(employeeId);
            resultMap.put("selectEmployee",selectEmployee);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //=============================================经销商控制============================================================
    /**
     * @title 新建经销商
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("saveDealerDo")
    @ResponseBody
    public Map<String,Object> saveDealerDo(Dealer dealer){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = dealerService.saveDealer(dealer);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 修改经销商信息
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("updateDealerDo")
    @ResponseBody
    public Map<String,Object> updateDealerDo(Dealer dealer){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = dealerService.updateDealer(dealer);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 根据经销商的编号删除
     * @description 软删除
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("removeDealerDo")
    @ResponseBody
    public Map<String,Object> removeDealerDo(Dealer dealer){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = dealerService.removeDealerDo(dealer);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 经销商列表查询
     * @description 组合查询，分页查询
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectDealerList")
    @ResponseBody
    public Map<String,Object> selectDealerList(LayUiPageBean pageBean, Dealer dealer, @DateTimeFormat(pattern = "yyyy-MM-dd") Date staData ,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){

        Map<String,Object> resultMap = null;
        try{
            List<Dealer> dealerList = dealerService.selectDealerList(pageBean, dealer, staData, endDate);
            Integer count=dealerService.getCount(dealer, staData, endDate);
            resultMap = LayUiUtil.getLayTableResponseMap("success", 0, count, dealerList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap = LayUiUtil.getLayTableResponseMap("failed", 400, 0, null);
        }
        return resultMap;
    }
    /**
     * @title 查询经销商
     * @description 根据编号查询经销商
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectDealerDo")
    @ResponseBody
    public Map<String,Object> selectDealerDo(Integer dealerId){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Dealer dealer = dealerService.selectDealer(dealerId);
            resultMap.put("dealer",dealer);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //=============================================医院负责人控制========================================================
    /**
     * @title 新增医院负责人
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("saveHospitalManagerDo")
    @ResponseBody
    public Map<String,Object> saveHospitalManagerDo(HospitalManager hospitalManager){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = hospitalManagerService.addHospitalManager(hospitalManager);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 修改医院负责人信息
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("updateHospitalManagerDo")
    @ResponseBody
    public Map<String,Object> updateHospitalManagerDo(HospitalManager hospitalManager){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = hospitalManagerService.updateHospitalManager(hospitalManager);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 医院负责人删除
     * @description 软删除
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("delHospitalManagerDo")
    @ResponseBody
    public Map<String,Object> delHospitalManagerDo(HospitalManager hospitalManager){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = hospitalManagerService.delHospitalManager(hospitalManager);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 医院负责人列表查询
     * @description 条件组合查询，分页查询
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectHospitalManagerList")
    @ResponseBody
    public Map<String,Object> selectHospitalManagerList(LayUiPageBean pageBean,HospitalManager hospitalManager,@DateTimeFormat(pattern = "yyyy-MM-dd") Date staData ,@DateTimeFormat(pattern = "yyyy-MM-dd")  Date endDate){
        Map<String,Object> resultMap = null;
        try{
            List<HospitalManager> hospitalManagerList = hospitalManagerService.getHospitalManagerBySelect(pageBean,hospitalManager,staData,endDate);
            Integer count=hospitalManagerService.getCount(hospitalManager,staData,endDate);
            resultMap = LayUiUtil.getLayTableResponseMap("success", 0, count, hospitalManagerList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap = LayUiUtil.getLayTableResponseMap("failed", 400, 0, null);
        }
        return resultMap;
    }
    @RequestMapping("selectHospitalManager")
    @ResponseBody
    public Map<String,Object> selectHospitalManager(Integer hospitalManagerId){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            HospitalManager hospitalManager = hospitalManagerService.selectById(hospitalManagerId);
            resultMap.put("hospitalManager",hospitalManager);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //=============================================科室负责人控制========================================================
    /**
     * @title 新建部门负责人
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("saveDeptManagerDo")
    @ResponseBody
    public Map<String,Object> saveDeptManagerDo(DeptManager deptManager){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = deptManagerService.addDeptManager(deptManager);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 修改部门负责人信息
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("updateDeptManagerDo")
    @ResponseBody
    public Map<String,Object> updateDeptManagerDo(DeptManager deptManager){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = deptManagerService.updateDeptManager(deptManager);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 删除部门负责人
     * @description 软删除
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("delDeptManagerDo")
    @ResponseBody
    public Map<String,Object> delDeptManagerDo(DeptManager deptManager){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = deptManagerService.delDeptManager(deptManager);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 查询部门负责人列表
     * @description 条件组合查询，
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectDeptManagerList")
    @ResponseBody
    public Map<String,Object> selectDeptManagerList(LayUiPageBean pageBean,DeptManager deptManager, @DateTimeFormat(pattern = "yyyy-MM-dd") Date staData ,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){

        Map<String,Object> resultMap = null;
        try{
            List<DeptManager> deptManagerList = deptManagerService.getDeptManagerByselect(pageBean, deptManager, staData, endDate);
            Integer count=deptManagerService.getCount(deptManager, staData, endDate);
            resultMap = LayUiUtil.getLayTableResponseMap("success", 0, count, deptManagerList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap = LayUiUtil.getLayTableResponseMap("failed", 400, 0, null);
        }
        return resultMap;
    }
    /**
     * @title 查询部门负责人
     * @description 根据编号查询
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectDeptManager")
    @ResponseBody
    public Map<String,Object> selectDeptManager(Integer deptManagerId){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            DeptManager deptManager = deptManagerService.selectById(deptManagerId);
            resultMap.put("deptManager",deptManager);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
}
